//二叉搜索树第K小的元素--搜索、回溯、剪枝

class Solution {
public:
    int count, ret;
    int kthSmallest(TreeNode* root, int k) {
        count = k;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode *root)
    {
        if(root == nullptr || count == 0) return;
        dfs(root->left);
        if(--count == 0)
        {
            ret = root->val;
        }
        if(count == 0) return;
        dfs(root->right);
    }
};